package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.AnswerDao;
import com.zretc.support.dao.TopicmultDao;
import com.zretc.support.dao.TopicsingleDao;
import com.zretc.support.entity.AnsAndTop;
import com.zretc.support.entity.Answer;
import com.zretc.support.entity.Topicsingle;
import com.zretc.support.service.AnswerService;
import com.zretc.support.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerDao answerDao;
    // 这里用到的可以到时候service有了以后再用它的封装方法
    @Autowired
    TopicsingleDao topicsingleDao;
    @Autowired
    TopicmultDao topicmultDao;
    @Autowired
    LogService logService;

    @Override
    public List<Answer> allanswerList(int examId, String userId) {
        List<Answer> allanswerList = answerDao.findAnswerByExamIdAndUserId(examId,userId);
        return allanswerList;
    }

    @Override
    public List<Answer> answerList(int examId, String userId) {
        // 获取 某场考试的 某个学生的所有答题信息
        List<Answer> answerList = allanswerList(examId,userId);
        // 存储 某场考试的 某个学生的所有综合题答题信息
        List<Answer> Lastlist = new ArrayList<>();
        for (int i = 0; i < answerList.size(); i++) {
            String topicId = answerList.get(i).getTopicId();
            // 判断是否是试题表A的
            boolean flag = topicId.startsWith("A");
            if(flag){
                // 在试题表中查询 试题类型
                long topicType = topicsingleDao.findById(topicId).get().getTopicType();
                // 如果是综合题就加到集合中
                if (topicType == 3){
                    Lastlist.add(answerList.get(i));
                }
            }
        }
        return Lastlist;
    }

    // 统计客观题分数 这里有冗余 可以再优化
    @Override
    public double countObjective(int examId, String userId) {
        // 获取 某场考试的 某个学生的所有答题信息
        List<Answer> answerList = allanswerList(examId,userId);
        // 计分
        double count = 0.0;
        // 循环判断
        for (int i = 0; i < answerList.size(); i++) {
            String topicId = answerList.get(i).getTopicId();
            boolean flag = topicId.startsWith("A");
            boolean flag2 = topicId.startsWith("B");
            if(flag){
                long topicType = topicsingleDao.findById(topicId).get().getTopicType();
                if(topicType != 3){
                    String right = topicsingleDao.findById(topicId).get().getTopicAnswer();
                    String answer = answerList.get(i).getAnswerContent();
                    if(right.equals(answer)){
                        count = count+topicsingleDao.findById(topicId).get().getTopicScore();
                    }
                }
            }else if(flag2){
                String right = topicmultDao.findById(topicId).get().getChooseRight();
                String answer = answerList.get(i).getAnswerContent();
                if(right.equals(answer)){
                    count = count+topicmultDao.findById(topicId).get().getChooseScore();
                }
            }
        }
        return count;
    }

    // 前台教师给综合题打分  -- 修改
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    // propagation 传播特性  --> 方法1 调用 方法2 ，方法1有事务会影响到方法2吗?
    public int markSubjective(Answer answer) {
        // 根据答题编号获取信息
        Answer theAnswer = answerDao.findById(answer.getAnswerId()).get();
        theAnswer.setSocre(answer.getSocre());
        theAnswer = answerDao.save(theAnswer);
        if (theAnswer != null){
            return 1;
        }
        return 0;
    }


    @Override
    public int addAnswer(Answer answer) {
        answer = answerDao.save(answer);
        if (answer != null){
            return 1;
        }
        return 0;
    }

    /**
     * 获取某同学的综合题信息
     * @param examId
     * @param userId
     * @return
     */
    @Override
    public List<AnsAndTop> getAnswerMult(int examId, String userId) {
        //获取某同学所有题目答案
        List<Answer> list = answerDao.findAnswerByExamIdAndUserId(examId, userId);
        List<AnsAndTop> map = new ArrayList<AnsAndTop>();
        //筛选出综合题，封装成前台页面需要的数据
        for (Answer answer : list) {
            //综合题位于A表
            if (answer.getTopicId().startsWith("A")) {
                Topicsingle ts = topicsingleDao.findByTopicId(answer.getTopicId());
                //综合题 类型为3
                if (ts.getTopicType() == 3) {
                    AnsAndTop at = new AnsAndTop();
                    at.setAnswer(answer);
                    at.setTs(ts);
                    //存入map
                    map.add(at);
                }
            }
        }
        return map;
    }

    @Override
    public Answer getByExamId(long examId) {
        return answerDao.getByExamIds(examId);
    }
    @Override
    public Page<Answer> getByPage(String keywords,int page, int pageSize) {
        Pageable pageable = PageRequest.of(page,pageSize);
        return answerDao.page(keywords,pageable);
    }

    @Override
    public int updSocre(double socre, Integer answerId) {
        return answerDao.updScore(socre,answerId);
    }

    @Override
    public Answer getByAnswerId(Integer answerId) {
        return answerDao.findById(answerId).get();
    }
}
